package com.itheima.health.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.dao.CheckGroupItemMapper;
import com.itheima.health.dao.CheckGroupMapper;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.pojo.CheckGroupItem;
import com.itheima.health.service.CheckGroupService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@DubboService
@Slf4j
public class CheckGroupServiceImpl implements CheckGroupService {
    @Autowired
    private CheckGroupMapper checkGroupMapper;
    @Autowired
    private CheckGroupItemMapper checkGroupItemMapper;

    /**
     * @Author: 位帅帅
     * @Date: 14:38 2021/1/5
     * @Params: [queryPageBean]
     * @RerurnType: com.itheima.health.entity.PageResult
     * @Description: 分页查询检查组
    */
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        log.info("[检查组-分页查询]param: {}",queryPageBean);
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        Page<CheckGroup> page = checkGroupMapper.findPage(queryPageBean.getQueryString());
        PageResult pageResult = new PageResult(page.getTotal(),page.getResult());
        log.info("[检查组-分页查询]result: {}",pageResult);
        return pageResult;
    }

    /**
     * @Author: 位帅帅
     * @Date: 15:48 2021/1/5
     * @Params: [checkitemIds, checkGroup]
     * @RerurnType: void
     * @Description: 添加检查组
    */
    @Transactional
    @Override
    public void add(Integer[] checkitemIds, CheckGroup checkGroup) {
        log.info("[检查组-添加]checkitemIds: {}, checkGroup: {}",checkitemIds,checkGroup);
        checkGroupMapper.insertSelective(checkGroup);
        for (Integer checkitemId : checkitemIds) {
            CheckGroupItem checkGroupItem = new CheckGroupItem(checkGroup.getId(),checkitemId);
            checkGroupItemMapper.insertSelective(checkGroupItem);
        }
    }

    /**
     * @Author: 位帅帅
     * @Date: 16:52 2021/1/5
     * @Params: [id]
     * @RerurnType: com.itheima.health.pojo.CheckGroup
     * @Description: 根据id查询检查组
    */
    @Override
    public CheckGroup findById(Integer id) {
        log.info("[检查组-id查询]id: {}",id);
        CheckGroup checkGroup = checkGroupMapper.selectByPrimaryKey(id);
        log.info("[检查组-id查询]checkGroup: {}",checkGroup);
        return checkGroup;
    }

    /**
     * @Author: 位帅帅
     * @Date: 17:07 2021/1/5
     * @Params: [id]
     * @RerurnType: java.util.List<java.lang.Integer>
     * @Description: 根据检查组id查询所有检查项id
    */
    @Override
    public List<Integer> findCheckItemIdsByCheckGroupId(Integer id) {
        log.info("[检查组-根据检查组id查询所有检查项id]id: {}",id);
        List<Integer> checkItemIds = checkGroupItemMapper.findCheckItemIdsByCheckGroupId(id);
        log.info("[检查组-根据检查组id查询所有检查项id]checkItemIds: {}",checkItemIds);
        return checkItemIds;
    }

    /**
     * @Author: 位帅帅
     * @Date: 17:43 2021/1/5
     * @Params: [checkitemIds, checkGroup]
     * @RerurnType: void
     * @Description: 编辑检查者
    */
    @Transactional
    @Override
    public void edit(Integer[] checkitemIds, CheckGroup checkGroup) {
        log.info("[检查组-编辑]checkitemIds: {}, checkGroup: {}",checkitemIds,checkGroup);
        checkGroupMapper.updateByPrimaryKeySelective(checkGroup);
        checkGroupItemMapper.deleteCheckGroupItemByCheckGroupId(checkGroup.getId());
        for (Integer checkitemId : checkitemIds) {
            CheckGroupItem checkGroupItem = new CheckGroupItem(checkGroup.getId(), checkitemId);
            checkGroupItemMapper.insertSelective(checkGroupItem);
        }
    }

    /**
     * @Author: 位帅帅
     * @Date: 19:20 2021/1/5
     * @Params: [id]
     * @RerurnType: void
     * @Description: 删除检查组
    */
    @Transactional
    @Override
    public void delete(Integer id) {
        checkGroupItemMapper.deleteCheckGroupItemByCheckGroupId(id);
        checkGroupMapper.deleteByPrimaryKey(id);
    }

    /**
     * @Author: 位帅帅
     * @Date: 19:28 2021/1/6
     * @Params: []
     * @RerurnType: java.util.List<com.itheima.health.pojo.CheckGroup>
     * @Description: 查询全部检查组
    */
    @Override
    public List<CheckGroup> findAll() {
        List<CheckGroup> checkGroupList = checkGroupMapper.selectAll();
        log.info("[检查组-全部查询]checkGroupList: {}",checkGroupList);
        return checkGroupList;
    }
}
